/*
 * Copyright (c) 2006, 2021, webrx.cn All rights reserved.
 *
 */
package cn.thread;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantLock;

/**
 * <p>Project: javaseapp - T9
 * <p>Powered by webrx On 2021-08-27 11:35:04
 * <p>Created by IntelliJ IDEA
 *
 * @author webrx [webrx@126.com]
 * @version 1.0
 * @since 16
 */
public class T9 {
    int num = 0;
    ReentrantLock lock = new ReentrantLock(true);

    void work() {
        while (true) {
            lock.lock();
            try {
                TimeUnit.SECONDS.sleep(1);
                String tn = Thread.currentThread().getName();
                System.out.printf("%s%d ", tn, ++num);
                if ("D".equals(tn)) {
                    System.out.println();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            lock.unlock();
        }
    }

    public static void main(String[] args) {
        var t = new T9();
        new Thread(t::work, "A").start();
        new Thread(t::work, "B").start();
        new Thread(t::work, "C").start();
        new Thread(t::work, "D").start();

        // C1 A2 B3 D4 C5

    }
}
